package com.ruoyi.personnel.oa.note.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.oa.note.domain.Note;
import com.ruoyi.personnel.oa.note.domain.NoteCond;
import com.ruoyi.personnel.oa.note.service.INoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 视图层
 * 便签
 */
@RestController
@RequestMapping("/oa/note")
@Api(value = "我的便签模块", tags = "我的便签接口")
public class NoteController extends BaseController {
    @Autowired
    private INoteService noteService;
    @Autowired
    private TokenService tokenService;

    /**
     * 根据搜索条件所有便签记录
     * @return
     */
    @ApiOperation(value = "获取便签列表",notes = "查询所有便签信息")
    @PostMapping("/list")
    public TableDataInfo list() {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        List<Note> notes = noteService.selectNoteById(user.getUserId());
        return getDataTable(notes);
    }

    /**
     * 导出便签
     * @param cond
     * @return
     */
    @PostMapping("/export")
    @ApiOperation("导出便签")
    public AjaxResult export(@RequestBody NoteCond cond)
    {
        List<Note> notes = noteService.selectNoteList(cond);
        ExcelUtil<Note> util = new ExcelUtil<>(Note.class);
        return util.exportExcel(notes, "便签");
    }

    /**
     * 新增便签
     * @param note
     * @return
     */
    @ApiOperation(value = "新增便签", notes = "新增便签")
    @ApiImplicitParam(name = "note", value = "新增便签", dataType = "Note")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Note note) {
        return toAjax(noteService.insertNote(note));
    }

    /**
     * 修改便签
     * @param note
     * @return
     */
    @ApiOperation(value = "修改便签", notes = "修改便签")
    @ApiImplicitParam(name = "note", value = "修改便签", dataType = "Note")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody Note note) {
        return toAjax(noteService.updateNote(note));
    }

    /**
     * 删除便签
     * 仅修改删除标志
     * @param note
     * @return
     */
    @ApiOperation(value = "删除便签", notes = "删除便签,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Note note) {
        return toAjax(noteService.updateNoteDeleteFlag(note));
    }
}
